/*
 * 'Highly configurable' mutable plugin boilerplate
 * Author: @markdalgleish
 * Further changes, comments: @addyosmani
 * Licensed under the MIT license
 */


// Note that with this pattern, as per Alex Sexton's, the plugin logic
// hasn't been nested in a jQuery plugin. Instead, we just use
// jQuery for its instantiation.

;(function( $, window, document, undefined ){
	
	// our plugin constructor
	var RadiCMS_Slug = function( elem, options ){
		this.elem = elem;
		this.$elem = $(elem);
		this.options = options;
		
		// This next line takes advantage of HTML5 data attributes
		// to support customization of the plugin on a per-element
		// basis. For example,
		// <div class=item' data-plugin-options='{"message":"Goodbye World!"}'></div>
		this.metadata = this.$elem.data( 'plugin-options' );
	};
	
	// the plugin prototype
	RadiCMS_Slug.prototype = {
		defaults: {
			input: '#input',
			onInit: true,
			onChange: true
		},
	
		init: function() {
			// Introduce defaults that can be extended either 
			// globally or using an object literal. 
			this.config = $.extend({}, this.defaults, this.options, this.metadata);
			if(this.config.onInit)
			{
				this.doSlug();
			}
			
			if(this.config.onChange)
			{
				this.bindOnChange();
			}
			// Sample usage:
			// Set the message per instance:
			// $('#elem').plugin({ message: 'Goodbye World!'});
			// or
			// var p = new Plugin(document.getElementById('elem'), 
			// { message: 'Goodbye World!'}).init()
			// or, set the global default message:
			// Plugin.defaults.message = 'Goodbye World!'
			
			//this.sampleMethod();
			return this;
		},
		
		doSlug: function()
		{
			this.$elem.text(this.generateSlug(this.getInputValue()));
		},
		
		bindOnChange: function()
		{
			var that = this;
			var input = $(this.config.input);
			if(input.is('select'))
			{
				input.change(function(e) {
                	that.doSlug();
            	});
			}
			else
			{
				input.keyup(function(e) {
                    that.doSlug();
                });
			}			
		},
		
		getInputValue: function()
		{
			var input = $(this.config.input);
			if(input.is('select'))
			{
				return input.find('option:selected').html();	
			}
			else
			{
				return input.val();
			}
		},
		
		generateSlug: function(slugcontent) {
			slugcontent = slugcontent.replace('- ', '');
			var slugcontent_hyphens = slugcontent.replace(/\s/g,'-');
			var finishedslug = slugcontent_hyphens.replace(/[^a-zA-Z0-9\-]/g,'');	
			return finishedslug;
		},
		
		sampleMethod: function() {
		// eg. show the currently configured message
		// console.log(this.config.message);
		}
	}
	
	RadiCMS_Slug.defaults = RadiCMS_Slug.prototype.defaults;
	
	$.fn.RadiCMS_Slug = function(options) {
		return this.each(function() {
			new RadiCMS_Slug(this, options).init();
		});
	};
	
	//optional: window.Plugin = Plugin;

})( jQuery, window , document );
